package com.abc.chatprj;

import jakarta.servlet.*;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.*;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/chat/users")
public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       request.setCharacterEncoding("UTF-8");
       response.setCharacterEncoding("UTF-8");
       response.setContentType("text/html;charset=UTF-8");

       PrintWriter out = response.getWriter();

        Object me = request.getSession().getAttribute("myself");
        Object obj = this.getServletContext().getAttribute("users");
        out.print("<html><meta http-equiv='refresh' content='1'>");
        out.print("<body>");
        if(obj != null){
            List<String> list = (List<String>)obj;
            for(String name : list){
                if(name.equals(me)){
                    out.print("<font color='blue'>" + name + "</font><br>");
                }else {
                    out.print(name + "<br>");
                }
            }
            out.print("</body></html>");
            out.flush();
        }
        out.close();
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
